package graduate.cslg.com.universityshopping.dormitory;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import graduate.cslg.com.universityshopping.R;
import graduate.cslg.com.universityshopping.net.LoginResponse;
import graduate.cslg.com.universityshopping.net.RetrofitDevelop;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class OnlineRepairActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText userTel;
    private EditText userPlace;
    private EditText repairContent;
    private EditText repairExtra;
    private Spinner nameSpinner;
    private Spinner publicSpinner;
    private String[] typeName = {"水工类", "电工类", "木工类", "空调类"};
    private String[] isPublic = {"公开", "非公开"};
    private String getTypeName;
    private String getPublicName;
    private String getAccount;
    private Button submitButton;
    private LoginResponse mLoginResponse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_online_repair);

        SharedPreferences preferences = this.getSharedPreferences("user_info",MODE_PRIVATE);
        getAccount= preferences.getString("user_account", "Z09315136");
        initViews();
    }

    private void initViews() {
        userTel = findViewById(R.id.stu_phone);
        userPlace = findViewById(R.id.stu_place);
        repairContent = findViewById(R.id.stu_wrong);
        repairExtra = findViewById(R.id.stu_more);
        nameSpinner = findViewById(R.id.repair_item);
        publicSpinner = findViewById(R.id.is_public);
        submitButton = findViewById(R.id.submit_button);
        submitButton.setOnClickListener(this);
        ArrayAdapter<String> type_adapter = new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_dropdown_item,typeName);
        ArrayAdapter<String> public_adapter = new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_dropdown_item,isPublic);
        nameSpinner.setAdapter(type_adapter);
        nameSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                getTypeName = typeName[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        publicSpinner.setAdapter(public_adapter);
        publicSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                getPublicName = isPublic[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.submit_button:
                String getTel = userTel.getText().toString();
                String getPlace = userPlace.getText().toString();
                String content = repairContent.getText().toString();
                String extra = repairExtra.getText().toString();
                if (getPlace.length() == 0 || getTel.length() == 0 || content.length() == 0 || extra.length() == 0) {
                    Toast.makeText(this, "请检查输入！", Toast.LENGTH_SHORT).show();
                } else {
                    postRepairInfo(getAccount, getTel, getPlace, getTypeName, content, getPublicName, extra);
                }
                break;
        }
    }

    private void postRepairInfo(String userAccount, String userTel, String repairPlace, String repairItem, String repairIntro, String repairPublic, String repairExtra) {
        RetrofitDevelop.getTestService()
                .postRepairInfo(userAccount, userTel, repairPlace, repairItem, repairIntro, repairPublic, repairExtra)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginResponse>() {
                    @Override
                    public void onCompleted() {
                        if (mLoginResponse.getStatus().equals("true")) {
                            System.out.println("提交成功！");
                            finish();
                        } else {
                            Toast.makeText(OnlineRepairActivity.this, "提交失败，请检查输入", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        Toast.makeText(OnlineRepairActivity.this, "网络错误！", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(LoginResponse loginResponse) {
                        mLoginResponse = loginResponse;
                    }
                });
    }
}
